package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.entity.ArticleFavorites;
import com.example.mapper.ArticleFavoritesMapper;
import com.example.service.ArticleFavoritesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class ArticleFavoritesController {

    @Autowired
    private ArticleFavoritesMapper articleFavoritesMapper;
    @GetMapping("/getArticleFavorites/{userId}")
    public List<ArticleFavorites> getFavoritesByUserId(@PathVariable Integer userId) {
        return articleFavoritesMapper.selectList(new QueryWrapper<ArticleFavorites>().eq("user_id", userId));
    }

    // 新增收藏文章
    @PostMapping("/addArticleFavorite")
    public String addArticleFavorite(@RequestBody ArticleFavorites articleFavorites) {
        int result = articleFavoritesMapper.insert(articleFavorites);
        if (result > 0) {
            return "收藏成功";
        } else {
            return "收藏失败";
        }
    }

    // 根据 userId 和 articleId 删除收藏文章
    @Autowired
    private ArticleFavoritesService articleFavoritesService;

    @DeleteMapping("/delete-favorite")
    public String deleteFavorite(@RequestParam Integer userId, @RequestParam Integer articleId) {
        boolean result = articleFavoritesService.deleteByUserIdAndArticleId(userId, articleId);
        if (result) {
            return "删除成功";
        } else {
            return "删除失败";
        }
    }

}

